<template>
  <div class="lin-container">
    <div class="lin-wrap">
      <div class="search-box">
        <div>
          <span class="search-title">类型</span>
          <el-select v-model="type" placeholder="请选择">
            <el-option
              v-for="item in selectList"
              :label="item.indexTitle"
              :value="item.id"
              :key="item.id"
            />
          </el-select>
        </div>

        <div class="operatiion">
          <el-button type="primary" size="small" @click="search">查询</el-button>
          <el-button size="small" @click="rest">重置</el-button>
        </div>
      </div>
      <div class="wrap-label">
        <span class="label">列表</span>
        <div class="operatiion">
          <el-button type="primary" size="small" @click="handleClick('新增')">新增</el-button>
        </div>
      </div>
      <el-table
        ref="table"
        class="tableClass"
        :data="tableData"
        style="width: 100%"
        :height="tableHeight"
        border
      >
        <el-table-column label="No" type="index" width="60"/>
        <el-table-column prop="indexTitle" label="名称"/>
        <el-table-column label="类型">
          <template #default="scope">
            <div>{{returnType(scope.row.parentId)}}</div>
          </template>
        </el-table-column>
         <el-table-column prop="indexTitle" label="缩略图">
           <template #default="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="returnImage(scope.row.indexImg)"
              :zoom-rate="1.2"
              fit="cover"
              @click="showBigImage(scope.row.indexImg)"
            />
          </template>
         </el-table-column>
        <el-table-column prop="createTime" label="发布时间">
         <template #default="scope">
            <div>{{getTime(scope.row.createTime)}}</div>
          </template>
          
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button link type="primary" size="small" @click="handleClick('编辑',scope.row)">修改</el-button>
            <el-button link type="danger" plain size="small" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="prev, pager, next,total,sizes"
        :page-sizes="[20,50, 100]"
        :page-size="pageConfig.pageSize"
        :total="pageConfig.total"
        @size-change="hanldleSizeChange"
        @current-change="handleCurrentChange"
        :hide-on-single-page="true"
        popper-class="admin-popper"
      />
    </div>
    <el-image-viewer
      hide-on-click-modal
      @close="()=>{showViewer = false}"
      v-if="showViewer && viewFileList.length>0"
      :url-list="viewFileList"
      :initial-index="0"
    />
  </div>
</template>

<script>
import {
  getNews,
  deleteMenu,
  addOrUpdateMenu,
  menuGetModuleContent
} from "@/api/PC-Home.js";
import { ElMessage, ElMessageBox } from "element-plus";
import Base64 from "@/utils/queryCode.js";
export default {
  name: "ImgsUploadStage",
  components: {},
  data() {
    return {
      form: {
        label: "",
        name: "",
        indexSort: ""
      },
      dialogTitle: "",
      dialogFormVisible: false,
      formLabelWidth: "140px",
      rules: {
        maxSize: 1,
        minWidth: 100,
        minHeight: 100
      },
      tableData: [],
      showViewer: false,
      viewFileList: [],
      queryId: null,
      file: null,
      imageFileList: [],
      // 搜索
      type: "",
      pageConfig: {
        total: 100,
        pageSize: 20,
        pageNum: 1
      },
      tableHeight: 0,
      selectList: []
    };
  },
  mounted() {
    this.getQueryId();
    this.getNews();
    this.menuGetModuleContent();
    this.tableHeightChange();
  },
  methods: {
    returnImage(url) {
      return process.env.VUE_APP_URL + "/" + url;
    },
    getQueryId() {
      this.queryId = this.$route.query.queryId;
    },
    search() {
      this.getNews();
    },
    rest() {
      this.type = "";
      this.getNews();
    },
    hanldleSizeChange(val) {
      this.pageConfig.pageSize = val
      this.getNews()
    },
    handleCurrentChange(val) {
      this.pageConfig.pageNum = val
      this.getNews()
    },
    handleClick(str, scope) {
      this.$router.push({
        path: "/addArticle",
        query: {
          parentId: Base64.encode(this.queryId),
          id: scope && scope.id?Base64.encode(scope.id):'',
          type: Base64.encode("business")
        }
      });
    },
    showBigImage(url) {
      this.viewFileList = [];
      this.showViewer = true;
      this.viewFileList.push(process.env.VUE_APP_URL + "/" + url);
    },
    getForm(value) {
      console.log(value);
      this.file = value;
    },
    submit() {
      let file = this.file;
      let form = null;
      form = new FormData();
      form.append("file", file ? file.raw : "");
      form.append("parentId", this.queryId);
      form.append("indexTitle", this.form.label);
      form.append("indexSort", this.form.indexSort);
      form.append("id", this.form.id ? this.form.id : "");
      addOrUpdateMenu(form).then(res => {
        if (res.code === 200) {
          this.$message.success("上传成功");
          this.getNews();
          this.dialogFormVisible = false;
        } else {
          this.$message.error(res.message);
        }
      });
    },
    getNews() {
      let param = {
        id: this.queryId,
        parentId: this.type,
        current: this.pageConfig.pageNum,
        size: this.pageConfig.pageSize
      };
      getNews(param).then(res => {
        this.tableData = res.data.records;
        this.pageConfig.total = res.data.total
      });
    },
    menuGetModuleContent() {
      let param = {
        id: 402
      };
      menuGetModuleContent(param).then(res => {
        this.selectList = res.data;
      });
    },
    returnType(id) {
      if(!id) return  
      let arr = this.selectList.filter(item => {
        return item.id === id;
      });
      return arr&& arr.length>0?arr[0].indexTitle:'';
    },
    getTime(timestamp) {
      if(!timestamp) return 
      let date = new Date(new Date().getTime(timestamp)); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      let Y = date.getFullYear(),
        M =
          date.getMonth() + 1 < 10
            ? "0" + (date.getMonth() + 1)
            : date.getMonth() + 1,
        D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate()
        // h = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
        // m =
        //   date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
        // s =
        //   date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      return Y + "-" + M + "-" + D;
    },
    handleDelete(item) {
      ElMessageBox.confirm("是否删除该条信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let param = {
            id: item.id,
            parentId: item.parentId
          };
          deleteMenu(param).then(res => {
            if (res.code === 200) {
              ElMessage({
                type: "success",
                message: "删除成功"
              });
              this.getNews();
            }
          });
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: "取消删除"
          });
        });
    },
    // 表格自定义高度
    tableHeightChange() {
      let clientHeight = document.documentElement.clientHeight;
      this.tableHeight =
        clientHeight -
        document.querySelector(".tableClass").getBoundingClientRect().top -
        140;
    }
  }
};
</script>

    
<style scoped lang="scss">
.lin-wrap {
  .wrap-label {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
    .label {
      font-size: 16px;
    }
  }
}
::v-deep .uploadimage {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  .el-form-item__content {
    display: flex;
    flex-direction: column;
    align-items: flex-start;

    .upload-demo {
      display: flex !important;
      flex-direction: row !important;
      align-items: center !important;

      .el-upload {
        margin-right: 10px;
      }
    }
    .demo-image__preview {
      display: flex;
      flex-direction: row;
    }
  }
}
::v-deep .el-dialog__header {
  display: flex;
  .el-dialog__headerbtn {
    top: 0;
  }
}
::v-deep .el-pagination {
  margin-top: 20px;
}
</style>
